<template>
  <div class="bg">

    <div class="content shadow">
      <div style="text-align: center;font-weight: bold;margin-bottom: 40px;font-size: 20px;">安徽好样CRM</div>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account" label-position="top"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off" label-position="top">
          </el-input>
        </el-form-item>
        <el-form-item style="margin-bottom: 0;">
          <el-button class="btn" type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button class="btn" @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
  import { Message } from 'element-ui';
  import {
    mapState,
    mapMutations
  } from 'vuex';
  export default {
    data() {
      return {
        ruleForm: {
          account: '',
          password: '',
          type: 1
        },
        rules: {
          account: [{
            required: true,
            message: '账号不能为空',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          }],
        }
      }
    },

    methods: {
      ...mapMutations(['ADD_USER']),
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.axios.post("/index/login", this.ruleForm).then(res => {
                this.ADD_USER(res);
                Message.success({
                  message: "成功",
                  onClose: (res) => {
                    console.log(123)
                    this.$router.replace({
                      path: "/index"
                    }, () => {
                      location.reload()
                    });
                  }
                })
              })
              .catch(error => {})
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
  }
</script>

<style>
  .bg{
      background:#0076D3 ;

  }
  .content {
    margin: auto;
    width: 400px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 50px;
    z-index: 999;
    background: #FFFFFF;
    box-shadow: 0px 2px 8px rgba(170, 170, 170, 0.3);
    border-radius: 20px;
    margin: auto;
  }

  .btn {
    width: 100px;
  }
</style>
